package lew.bing.exp.metrics;

import com.codahale.metrics.ConsoleReporter;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;

import java.util.concurrent.TimeUnit;

/**
 * Created by 刘国兵 on 2018/7/14.
 */
public class GetStarted {

    static final MetricRegistry metrics = new MetricRegistry();
    public static void main(String args[]) {
        startReport();
        Meter requests = metrics.meter("requests");

        wait5Seconds(requests);
    }

    static void startReport() {
        ConsoleReporter reporter = ConsoleReporter.forRegistry(metrics)
                .convertRatesTo(TimeUnit.SECONDS)
                .convertDurationsTo(TimeUnit.MILLISECONDS)
                .build();
        reporter.start(1, TimeUnit.SECONDS);// 1s报道一次
    }

    static void wait5Seconds(Meter request) {
        // 每隔100ms一次
        int times = 0;
        try {
            while ((times ++) < 50) {
                request.mark();
                Thread.sleep(100);
            }
        }
        catch(InterruptedException e) {}
    }

}
